//
//  HotRecommendTableViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/26.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class HotRecommendTableViewCell: WZC_TableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    var model:RecommendModel?
    {
        didSet
        {
            if (oldValue != nil) {
                
            }
            else
            {
                
                self.gridView.reloadData()
            }

        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    lazy var gridView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        
        layout.scrollDirection = UICollectionView.ScrollDirection.vertical
        layout.itemSize = CGSize.init(width: (kScreenW-10*3)/2, height:64.0*4.5)
        
        let collectionView = UICollectionView.init(frame:.zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.hexColor(hexadecimal: "#f2f2f2")
        collectionView.register(HotRecommendCollectionViewCell.self, forCellWithReuseIdentifier: "HotRecommendCollectionViewCell")
        collectionView.isScrollEnabled = false;
        self.contentView.addSubview(collectionView)
        
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        gridView.snp.makeConstraints { (make) in
            
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HotRecommendTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return model?.data?.count ?? 0
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:HotRecommendCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HotRecommendCollectionViewCell", for: indexPath) as! HotRecommendCollectionViewCell
        cell.model = model?.data?[indexPath.item]
        return cell
    }
    
}
